
package com.bangcommunity.bbframe.springredis;

import com.bangcommunity.bbframe.common.utils.lang.StringUtils;
import com.bangcommunity.bbframe.dynconfig.DynConfigClientFactory;
import com.bangcommunity.bbframe.dynconfig.IDynConfigProcessor;
import com.bangcommunity.bbframe.dynconfig.annotation.ZkConf;
import com.bangcommunity.bbframe.dynconfig.annotation.ZkNodekey;
import lombok.Data;

import java.io.Serializable;

/**
 * Created by tanghc on 17/1/13.
 *
 */
@Data
@ZkConf(group = "common", dataId = "redisConfig")
public class ProjectRedisClientConf implements Serializable,IDynConfigProcessor {
    private String config;
    @ZkNodekey("redis.host")
    private String host;
    @ZkNodekey("redis.port")
    private Integer port;
    @ZkNodekey("redis.password")
    private String password;
    @ZkNodekey("redis.db")
    private String db;
    @ZkNodekey("redis.timeout")
    private Integer timeout=2000;
    @ZkNodekey("redis.usePool")
    private Boolean usePool=true;
    @ZkNodekey("redis.pool.maxIdle")
    private Integer maxIdle=10;
    @ZkNodekey("redis.pool.maxTotal")
    private Integer maxTotal=30;
    @ZkNodekey("redis.pool.maxWaitMillis")
    private Integer maxWaitMillis=15000;
    @ZkNodekey("redis.pool.minEvictableIdleTimeMillis")
    private Integer minEvictableIdleTimeMillis=1800000;
    @ZkNodekey("redis.pool.minIdle")
    private Integer minIdle=0;
    @ZkNodekey("redis.pool.numTestsPerEvictionRun")
    private Integer numTestsPerEvictionRun=1024;
    @ZkNodekey("redis.pool.testOnBorrow")
    private Boolean testOnBorrow=true;
    @ZkNodekey("redis.pool.testOnReturn")
    private Boolean testOnReturn=false;
    @ZkNodekey("redis.pool.testWhileIdle")
    private Boolean testWhileIdle=false;
    @ZkNodekey("redis.pool.timeBetweenEvictionRunsMillis")
    private Integer timeBetweenEvictionRunsMillis=30000;
    @ZkNodekey("redis.useNameSpace")
    private Boolean useNameSpace=true;
    @ZkNodekey("redis.url")
    private String url;
    @ZkNodekey("redis.sentinel")
    private String sentinelUrl;
    @ZkNodekey("redis.sentinel.masterName")
    private String sentinelMasterName;
    @ZkNodekey("redis.cluster.nodes")
    private String clusterNodes;
    @ZkNodekey("redis.cluster.maxRedirects")
    private Integer clusterMaxRedirects;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ProjectRedisClientConf that = (ProjectRedisClientConf) o;

        if (host != null ? !host.equals(that.host) : that.host != null) return false;
        if (port != null ? !port.equals(that.port) : that.port != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (timeout != null ? !timeout.equals(that.timeout) : that.timeout != null) return false;
        if (usePool != null ? !usePool.equals(that.usePool) : that.usePool != null) return false;
        if (maxIdle != null ? !maxIdle.equals(that.maxIdle) : that.maxIdle != null) return false;
        if (maxTotal != null ? !maxTotal.equals(that.maxTotal) : that.maxTotal != null) return false;
        if (maxWaitMillis != null ? !maxWaitMillis.equals(that.maxWaitMillis) : that.maxWaitMillis != null)
            return false;
        if (minEvictableIdleTimeMillis != null ? !minEvictableIdleTimeMillis.equals(that.minEvictableIdleTimeMillis) : that.minEvictableIdleTimeMillis != null)
            return false;
        if (minIdle != null ? !minIdle.equals(that.minIdle) : that.minIdle != null) return false;
        if (numTestsPerEvictionRun != null ? !numTestsPerEvictionRun.equals(that.numTestsPerEvictionRun) : that.numTestsPerEvictionRun != null)
            return false;
        if (testOnBorrow != null ? !testOnBorrow.equals(that.testOnBorrow) : that.testOnBorrow != null) return false;
        if (testOnReturn != null ? !testOnReturn.equals(that.testOnReturn) : that.testOnReturn != null) return false;
        if (testWhileIdle != null ? !testWhileIdle.equals(that.testWhileIdle) : that.testWhileIdle != null)
            return false;
        if (timeBetweenEvictionRunsMillis != null ? !timeBetweenEvictionRunsMillis.equals(that.timeBetweenEvictionRunsMillis) : that.timeBetweenEvictionRunsMillis != null)
            return false;
        if (useNameSpace != null ? !useNameSpace.equals(that.useNameSpace) : that.useNameSpace != null) return false;
        if (url != null ? !url.equals(that.url) : that.url != null) return false;
        if (sentinelUrl != null ? !sentinelUrl.equals(that.sentinelUrl) : that.sentinelUrl != null) return false;
        if (sentinelMasterName != null ? !sentinelMasterName.equals(that.sentinelMasterName) : that.sentinelMasterName != null)
            return false;
        if (clusterNodes != null ? !clusterNodes.equals(that.clusterNodes) : that.clusterNodes != null) return false;
        return !(clusterMaxRedirects != null ? !clusterMaxRedirects.equals(that.clusterMaxRedirects) : that.clusterMaxRedirects != null);

    }

    @Override
    public int hashCode() {
        int result = host != null ? host.hashCode() : 0;
        result = 31 * result + (port != null ? port.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (timeout != null ? timeout.hashCode() : 0);
        result = 31 * result + (usePool != null ? usePool.hashCode() : 0);
        result = 31 * result + (maxIdle != null ? maxIdle.hashCode() : 0);
        result = 31 * result + (maxTotal != null ? maxTotal.hashCode() : 0);
        result = 31 * result + (maxWaitMillis != null ? maxWaitMillis.hashCode() : 0);
        result = 31 * result + (minEvictableIdleTimeMillis != null ? minEvictableIdleTimeMillis.hashCode() : 0);
        result = 31 * result + (minIdle != null ? minIdle.hashCode() : 0);
        result = 31 * result + (numTestsPerEvictionRun != null ? numTestsPerEvictionRun.hashCode() : 0);
        result = 31 * result + (testOnBorrow != null ? testOnBorrow.hashCode() : 0);
        result = 31 * result + (testOnReturn != null ? testOnReturn.hashCode() : 0);
        result = 31 * result + (testWhileIdle != null ? testWhileIdle.hashCode() : 0);
        result = 31 * result + (timeBetweenEvictionRunsMillis != null ? timeBetweenEvictionRunsMillis.hashCode() : 0);
        result = 31 * result + (useNameSpace != null ? useNameSpace.hashCode() : 0);
        result = 31 * result + (url != null ? url.hashCode() : 0);
        result = 31 * result + (sentinelUrl != null ? sentinelUrl.hashCode() : 0);
        result = 31 * result + (sentinelMasterName != null ? sentinelMasterName.hashCode() : 0);
        result = 31 * result + (clusterNodes != null ? clusterNodes.hashCode() : 0);
        result = 31 * result + (clusterMaxRedirects != null ? clusterMaxRedirects.hashCode() : 0);
        return result;
    }

    public static ProjectRedisClientConf projectRedisClientConf;
    public static ISpringRedisCache iSpringRedisCache;
    static {
        projectRedisClientConf = DynConfigClientFactory.getClient().proxyConfigBean(ProjectRedisClientConf.class);
    }



    @Override
    public void procesConfig(String config) {
        if(!StringUtils.equalsIgnoreCase(config,this.config)){
            refreshRedisCache();
        }
    }
    private void refreshRedisCache(){
        if(null != ProjectRedisClientConf.iSpringRedisCache){
            ProjectRedisClientConf.iSpringRedisCache.destroy();
            ProjectRedisClientConf.iSpringRedisCache = ProjectRedisClientBuilder.getSpringRedisCache(this);
        }
    }

}
